package com.css.fxfzcpxx.modules.thematicMap.controller;


import com.alibaba.fastjson.JSONObject;
import com.css.fxfzcpxx.base.log.Log;
import com.css.fxfzcpxx.base.response.RestResponse;

import com.css.fxfzcpxx.modules.thematicMap.entity.AchManageEntity;
import com.css.fxfzcpxx.modules.thematicMap.entity.ThematicMap;
import com.css.fxfzcpxx.modules.thematicMap.param.Quaparams;
import com.css.fxfzcpxx.modules.thematicMap.param.QueParams;
import com.css.fxfzcpxx.modules.thematicMap.param.ZoningParams;
import com.css.fxfzcpxx.modules.thematicMap.service.TheMapService;

import com.css.fxfzcpxx.modules.thematicMap.vo.ListVo;
import com.css.fxfzcpxx.modules.thematicMap.vo.OtherSystemInfoVo;
import com.css.fxfzcpxx.modules.thematicMap.vo.ThematicMapVo;
import com.css.fxfzcpxx.modules.websocket.WebSocketManager;
import com.css.fxfzcpxx.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;
import java.util.Map;

/**
 * @author
 * @program
 * @description
 * @create 2022/8/8 10:39
 **/
@RestController
@RequestMapping("/theMap")
@Slf4j
public class TheMapController {
    @Resource
    TheMapService theMapService;

    private final WebSocketManager webSocketManager;

    @Autowired
    public TheMapController(WebSocketManager webSocketManager) {
        this.webSocketManager = webSocketManager;
    }

    /***
     * 分页查询
     * @param queParams
     * @return
     */
    @GetMapping("/page")
    public RestResponse getPage(QueParams queParams, HttpServletRequest request){
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=theMapService.getPage(queParams,curPage,pageSize);
            response = RestResponse.succeed(page);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 保存
     *
     * @param thematicMap
     * @return
     */
    @PostMapping("/save")
    public RestResponse save(@RequestBody ThematicMap thematicMap) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            theMapService.save(thematicMap);
            jsonObject.put("message", "保存成功");
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "保存失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 撤回/发布
     *
     * @param queParams
     * @return
     */
    @PostMapping("/recallOrPublish")
    public RestResponse recallOrPublish(@RequestBody QueParams queParams) {
        RestResponse response = null;
        try {
            theMapService.recallOrPublish(queParams);
            if("1".equals(queParams.getReleaseStatus())){
                webSocketManager.broadcastMessage("专题图发布啦","theMap");
            }else{
                webSocketManager.broadcastMessage("专题图撤回啦","theMap");
            }
        } catch (Exception e) {
            String errorMessage="";
            if("1".equals(queParams.getReleaseStatus())){
                errorMessage = "发布失败";
            }else{
                errorMessage = "撤回失败";
            }
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 删除
     * @param
     * @return
     */
    @PostMapping("/delete/{ids}")
    public RestResponse delete(@PathVariable("ids") String ids){
        RestResponse response = null;
        try{
            theMapService.delete(ids);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public RestResponse findById(@RequestParam("id") String id) {
        RestResponse response = null;
        try {
            ThematicMapVo infAchievement=theMapService.findById(id);
            response = RestResponse.succeed(infAchievement);
        } catch (Exception e) {
            String errorMessage = "查看失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 获取关键词管理数据
     * @param
     * @return
     */
    @GetMapping("/getkeywordManageData")
    public RestResponse getkeywordManageData(@RequestParam("id") String ids){
        RestResponse response = null;
        try{
            List<ThematicMap> dataList=theMapService.getkeywordManageData(ids);
            response = RestResponse.succeed(dataList);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 关键词修改
     * @param
     * @return
     */
    @PostMapping("/keywordUpdate")
    public RestResponse keywordUpdate(@RequestBody ListVo listvo){
        RestResponse response = null;
        try{
            theMapService.keywordUpdate(listvo);
            webSocketManager.broadcastMessage("专题图关键词修改啦","theMap");
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 修改
     * @param
     * @return
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody ThematicMap thematicMap){
        RestResponse response = null;
        try{
            theMapService.update(thematicMap);
            webSocketManager.broadcastMessage("专题图修改啦","theMap");
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 目录下载
     * @param
     * @return
     */
    @PostMapping("/catalogDownload/{ids}")
    public void catalogDownload(HttpServletResponse response, @PathVariable("ids") String ids){
        RestResponse restResponse = null;
        try{
            theMapService.catalogDownload(response,ids);
            //restResponse = RestResponse.succeed("下载成功");
        }catch (Exception e){
            String errormessage = e.getMessage();
            log.error(errormessage, e);
            //restResponse = RestResponse.fail(errormessage);
        }
        //return restResponse;
    }

    /***
     *
     * @param
     * @return
     */
    @GetMapping("/getDataList")
    @Log(value="专题图",type = "检索")
    public RestResponse getDataList(QueParams queParams, HttpServletRequest request){
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> dataList=theMapService.getDataList(queParams,curPage,pageSize);
            response = RestResponse.succeed(dataList);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 抗震加固工程分布图分页
     * @param
     * @return
     */
    @GetMapping("/getEngineeringDistributionPage")
    @Log(value="专题图",type = "检索")
    public RestResponse getEngineeringDistributionPage(HttpServletRequest request, Quaparams quaparams) {
        RestResponse response = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> list = theMapService.getEngineeringDistributionPage(quaparams,curPage,pageSize);
            response = RestResponse.succeed(list);
            response.setMessage("查询成功");
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    /***
     * 工程查看
     * @param
     * @return
     */
    @GetMapping("findEngineeringById")
    public RestResponse findEngineeringById(String id) {
        RestResponse restResponse = null;
        try {
            AchManageEntity achManageEntity = theMapService.findEngineeringById(id);
            restResponse = RestResponse.succeed(achManageEntity);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 获取工程上挂载的加固数据
     * @param
     * @return
     */
    @GetMapping("getData")
    public RestResponse getData(String ids) {
        RestResponse restResponse = null;
        try {
            List<Map<String,Object>> list = theMapService.getData(ids);
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 获取区划记录列表
     * @param
     * @return
     */
    @GetMapping("getZoningRecordList")
    public RestResponse getZoningRecordList(HttpServletRequest request, ZoningParams quaparams) {
        RestResponse response = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> list = theMapService.getZoningRecordList(quaparams,curPage,pageSize);
            response = RestResponse.succeed(list);
            response.setMessage("查询成功");
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 单指标风险区划设置-查询
     * @param
     * @return
     */
    @GetMapping("getZoningSetting")
    @Log(value="专题图",type = "检索")
    public RestResponse getZoningSetting(Integer type) {
        RestResponse restResponse = null;
        try {
            Map<String, Object> list = theMapService.getZoningSetting(type);
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 抗震加固工程分布图
     * @param
     * @return
     */
    @GetMapping("getEngineeringDistribution")
    public RestResponse getEngineeringDistribution(Quaparams quaparams) {
        RestResponse response = null;
        try {
            List<Map<String, Object>> list = theMapService.getEngineeringDistribution(quaparams);
            response = RestResponse.succeed(list);
            response.setMessage("查询成功");
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 通过工程类型获取对应案例集上挂载的加固数据
     * @param
     * @return
     */
    @GetMapping("getReinforceData")
    @Log(value="专题图",type = "检索")
    public RestResponse getReinforceData(Quaparams quaparams) {
        RestResponse restResponse = null;
        try {
            List<Map<String,Object>> list = theMapService.getReinforceData(quaparams);
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 对接其它系统
     * @return
     */
    @PostMapping("/dockingOtherSystem")
    public RestResponse dockingOtherSystem(OtherSystemInfoVo otherSystemInfoVo){
        RestResponse response = null;
        try{
            theMapService.dockingOtherSystem(otherSystemInfoVo);
            response = RestResponse.succeed();
            response.setMessage("保存成功");
        }catch (Exception e){
            String errorMessage = "保存失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
}
